package com.dingding.transfer.stamper.handler;

import com.alibaba.fastjson.JSONObject;
import com.dingding.transfer.stamper.entity.CompInfo;
import com.dingding.transfer.stamper.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangxiyuan
 * @description 事件处理组合器
 * @createDate 2021/3/30 15:07
 **/
@Component
@Slf4j
public class EventHandlerComposite implements  EventHandler{
    private List<EventHandler> eventHandlerList = new ArrayList<>();

    @Autowired
    private BusinessService businessService;

    @PostConstruct
    public void init(){
        eventHandlerList.add(new DefaultEventHandler());
        // 通过钉钉提供的另一个接口可以完整的获取到审批记录，不用每次都去监听审批回调接口，然后插入数据库
        //eventHandlerList.add(new TaskHandler());
        eventHandlerList.add(new TaskInstanceHandler());
        eventHandlerList.add(new UserAddHandler());
        eventHandlerList.add(new DeptRemoveHandler());
        eventHandlerList.add(new DeptAddHanlder());
    }


    @Override
    public boolean support(String eventType) {
        return false;
    }

    @Override
    public boolean handle(JSONObject jsonObject) {
        String corpId = jsonObject.getString("corpId");
        if(StringUtils.isNotBlank(corpId)){
            CompInfo compInfo = businessService.getCompInfo(corpId);
            if(compInfo == null){
                log.error("计算解密文字corpid不匹配");
                return false;
            }
        }
        for (EventHandler eventHandler :
                eventHandlerList) {
            if(eventHandler.support(jsonObject.getString("EventType"))){
                return eventHandler.handle(jsonObject);
            }
        }
        return false;
    }
}
